<?php


namespace app\admin\controller;


use http\Params;
use QL\QueryList;
use think\facade\Db;

class Search
{
    private $tableName, $pt, $data, $ptName = '';

    private $ptArr = [
        'default' => [
            'table_name' => 'video',
            'pt_name'    => '默认',
        ],
    ];

    public function __construct()
    {
        $this->pt        = request()->param('pt', 'default');
        $this->ptName    = $this->ptArr[$this->pt]['pt_name'];
        $this->tableName = $this->ptArr[$this->pt]['table_name'];
    }

    public function getSearch()
    {
        $type      = request()->param('type', '');
        $page      = request()->param('page', 1);
        $keywords  = request()->param('keywords', '');
        $sort      = request()->param('sort', 'asc');
        $pageLimit = 100;
        $start     = ($page - 1) * $pageLimit;
        $query     = Db::name($this->tableName);
        if (!empty($type) && $type != '全部') {
            $query->where('type', $type);
        }
        if (!empty($keywords)) {
            $query->where('title like \'%' . $keywords . '%\'');
        }
        $result = $query->limit($start, $pageLimit)
            ->order(['id' => $sort])
            ->select()->toArray();
        foreach ($result as $key => $item) {
            $result[$key]['is_show']     = 1;
            $result[$key]['create_time'] = date('Y-m-d', $item['create_time']);
        }
        $this->data = $result;
        return $this->responseData();
    }

    public function deleteMovie()
    {
        $id    = request()->param('id', 0);
        $query = Db::name($this->tableName)->where('id', '=', $id)->delete();
        if (!$query) {
            return json(array('status' => 400, 'message' => '请求失败！', 'data' => []));
        }
        return $this->responseData();
    }

    public function collection()
    {
        $id      = request()->param('id', 0);
        $is_like = request()->param('is_like', 0);
        Db::name($this->tableName)->where('id', '=', $id)->update(['is_like' => $is_like]);

        $data = Db::name($this->tableName)->field('id as video_id,type,cover,title,video,location,click_num,is_like,' . time() . ' as create_time')->find($id);
        if ($is_like == 1) {
            $data['pt'] = $this->pt;
            $data['pt_name'] = $this->ptName;
            Db::name('collection')->insert($data);
        } else {
            Db::name('collection')->where(['video_id' => $data['video_id'], 'pt' => $this->pt])->delete();

        }
        return $this->responseData();
    }

    public function info()
    {
        $id   = request()->param('id', 0);
        $info = Db::name($this->tableName)->where('id', '=', $id)->find();
        Db::table($this->tableName)
            ->where('id', $id)
            ->inc('click_num')
            ->update();
        $info['create_time'] = date('Y-m-d', $info['create_time']);
        $this->data          = $info;
        return $this->responseData();
    }

    public function getCollectionList()
    {
        $page      = request()->param('page', 1);
        $sort      = request()->param('sort', 'asc');
        $pageLimit = 100;
        $start     = ($page - 1) * $pageLimit;
        $query     = Db::name('collection');
        if (!empty($keywords)) {
            $query->where('title like \'%' . $keywords . '%\'');
        }
        $result = $query->limit($start, $pageLimit)
            ->order(['id' => $sort])
            ->select()->toArray();
        foreach ($result as $key => $item) {
            $result[$key]['is_show']     = 1;
            $result[$key]['create_time'] = date('Y-m-d', $item['create_time']);
        }
        $this->data = $result;
        return $this->responseData();
    }

    public function responseData()
    {
        return json([
            'status'  => 200,
            'message' => '请求成功！',
            'data'    => $this->data
        ]);
    }
}